#!/bin/sh

# 定义文件路径
BOXOTA_FILE="/data/local/qcom/log/boxotaLog.txt"

# 检查脚本权限并提醒设置权限
if [ ! -r "$BOXOTA_FILE" ]; then
    echo "错误：脚本无法访问必要文件，请将脚本文件设置为可读权限后再执行。"
    exit 1
fi

# 定义搜索关键字
KEYWORD="10000"

# 获取boxotaLog.txt文件内容的倒数10000行
BOXOTA_CONTENT=$(tail -n 10000 "$BOXOTA_FILE")

# 使用grep找到包含关键字的最后一行
BOXOTA_LINE=$(echo "$BOXOTA_CONTENT" | grep "$KEYWORD" | tail -n 1)

# 检查信息是否为空
if [ -z "$BOXOTA_LINE" ]; then
    echo "警告：无法获取有效的IP和端口信息。请稍后再试。"
    exit 1
fi

# 提取外网IP
EXTERNAL_IP=$(echo "$BOXOTA_LINE" | awk -F'"external":{"address":"' '{print $2}' | awk -F'","aport"' '{print $1}')

# 提取外网端口
EXTERNAL_PORT=$(echo "$BOXOTA_LINE" | awk -F'"external":{"address":"[^"]*","aport":' '{print $2}' | awk -F',"atype"' '{print $1}')

# 输出结果
echo "本机IP地址: $EXTERNAL_IP"
echo "端口映射信息："
echo "10000= $EXTERNAL_PORT"
echo "10001= $((EXTERNAL_PORT + 1))"
echo "10002= $((EXTERNAL_PORT + 2))"
echo "10003= $((EXTERNAL_PORT + 3))"
echo "10004= $((EXTERNAL_PORT + 4))"